<?php

namespace App\Http\Controllers\Sys_api;


use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Config;
use Illuminate\Http\Request;
use App\Models\CdOrderModel;
use App\Models\RechargeModel;
use App\Models\StationModel;
use App\Models\EquipmentPortModel;
use Overtrue\LaravelWeChat\EasyWeChat;


/**
 * 用户订单支付
 */
class PayAPIController extends BaseAPIController
{

    /**
     * 微信支付参数
     *
     * @return void
     */
    public function WxPayPara()
    {
        $options = Config::get("easywechat");
        $wxpaypara = [
            'notify_url' => $options["pay"]["default"]["notify_url"],
            'mchid' => $options["pay"]["default"]["merchant_id"],
            'apiKey' => $options["pay"]["default"]["key"]
        ];
        return $wxpaypara;
    }

    /**
     * 小程序参数
     *
     * @return void
     */
    public function WxMinPara()
    {
        $options = Config::get("easywechat");
        $wxMinpara = [
            'appid' => $options["mini_app"]["default"]["app_id"],
            'appsecret' => $options["mini_app"]["default"]["secret"]
        ];
        return $wxMinpara;
    }

    /**
     * 用户充值记录
     *
     * @return void
     */
    public function RechargeRecordsAPI(Request $request)
    {
        $page = $request->page;
        $map[] = ['user_id', '=', $request->input('uid')];
        $sort = ['id', 'desc'];
        $list = RechargeModel::UserRechargeRecords($this->pageSize, $page, $map, $sort);
        return $this->rjson($list);
    }

    /**
     * 用户支付订单
     *
     * @return void
     */
    public function StationPayAPI()
    {
        
        return false;
    }



    /**
     * 用户订单详情
     *
     * @return void
     */
    public function OrderDetailAPI(Request $request)
    {
        $id = $request->input('id');
        $total = 0;
        $res = CdOrderModel::UserOrderDetail($id);
        if ($res) $total = 1;
        $info = [
            'data' => $res,
            'total' => $total ?: 0
        ];
        return $this->rjson($info);;
    }


    /**
     * 用户结束订单
     *
     * @return void
     */
    public function EndOrderAPI(Request $request)
    {
        $page = $request->page;
        $cd_status = $request->cd_status;
        $type = $request->type;
        $map = [
            ['column' => 'user_id', 'operator' => '=', 'value' => session('user_id')],
            ['column' => 'cd_status', 'operator' => '=', 'value' => $cd_status]
        ];
        $sort = ['id', 'desc'];
        $list = CdOrderModel::GetCdOrderList($this->pageSize, $page, $map, $sort, $type);
        return $this->rjson($list);
    }


    /**
     * 用户订单列表
     *
     * @return void
     */
    public function OrderListAPI(Request $request)
    {
        $page = $request->page;
        $type = $request->type;
        $map[] = ['user_id', '=', $request->input('uid')];
        $sort = ['id', 'desc'];
        $data = [];
        $list = CdOrderModel::GetCdOrderList($this->pageSize, $page, $map, $sort, $type);
        if(!$list){
            return $this->rjson($list);
        }
        foreach($list['data'] as $key=>$value){
            //获取充电站信息根据station_id
            $station_info = StationModel::where('id',$value['station_id'])->first();
            //根据port_id获取充电枪编号
            $port_info = EquipmentPortModel::where('id',$value['port_id'])->first();
            $data[] = [
                'id' => $value['id'],
                'order_no' => $value['order_no'],
                'cd_status' => $value['cd_status'] == 0 ? '充电中':'结束充电',
                'start_time' => $value['start_time'],
                'stop_time' => $value['stop_time'],
                'cd_times' => $value['cd_times'],
                'cd_fei' => $value['cd_fei'],
                'port_id' => $port_info['port_id'],
                'station_name' => $station_info['station_name'],
                'station_adds' => $station_info['station_adds'],
            ];
        }
        $arr = [
            'total' => $list['total'],
            'data' => $data
        ];
        return $this->rjson($arr);
    }
}
